package com.sky.aspect;

import com.sky.anno.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: AutoFillAspect
 * USER: zhang
 * DATE: 2023/5/17
 * DESCRIPTION :对指定的方法进行增强的逻辑
 */
@Slf4j
@Component
@Aspect
public class AutoFillAspect {
    //用于匹配标记@AutoFill注解的方法
    @Pointcut("@annotation(com.sky.anno.AutoFill)")
    public void autoFillPointcut() {
    }

    @Before("autoFillPointcut()")
    public void beforeAutoFillMethod(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //获取原始方法的签名对象(包含原始方法的相关信息)
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        //基于表示原始方法的Method对象获取头上标记的相关注解内容
        AutoFill autoFill = method.getDeclaredAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();
        //基于joinPoint获取本次原始方法的实际参数
        Object[] args = joinPoint.getArgs();
        if (Objects.isNull(args) || args.length == 0) {
            return;
        }
        Object targetEntity = args[0];
        //准备要赋的实际数据(当前时间/当前登录用户的Id)
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        switch (operationType) {
            case INSERT:
                //反射获取到用于给目标对象字段赋值的SET方法的Method对象
                Method setCreateTime = targetEntity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = targetEntity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = targetEntity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = targetEntity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setCreateTime.invoke(targetEntity,now);
                setUpdateTime.invoke(targetEntity,now);
                setCreateUser.invoke(targetEntity,currentId);
                setUpdateUser.invoke(targetEntity,currentId);
                break;
            case UPDATE:
                Method updateTime = targetEntity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method updateUser = targetEntity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                updateTime.invoke(targetEntity,now);
                updateUser.invoke(targetEntity,currentId);
                break;
        }
    }


}
